import shutil
import os
import time
from selenium import webdriver
from bs4 import BeautifulSoup


def openUrl(url, floderName, fileName):
    #  通过 Selenium 模拟浏览器抓取，滑动进度条，抓取百度图片
    driver_path = r'E:\Application\chromedriver.exe'
    driver = webdriver.Chrome(executable_path=driver_path)
    driver.implicitly_wait(20)  # 隐形等待，最长等待20秒
    driver.get(url)
    time.sleep(4)
    # 通过page_source获取网页源代码
    # print(driver.page_source)
    # 滚动进度条5次，根据你的需要
    for i in range(0, 5):
        driver.execute_script('window.scrollTo(0,document.body.scrollHeight);')
        time.sleep(1)
    # 将网页内容读取保存至本地
    htmlText = driver.page_source
    if not os.path.exists(f"{floderName}"):
        os.makedirs(f"{floderName}")
    with open(f"{floderName}\\{fileName}.html", 'w+', encoding='utf-8') as f:
        f.write(htmlText)
    time.sleep(5)


def parsingHTML(floderName, fileName):
    with open(f"{floderName}\\{fileName}.html", 'rb+') as f:
        htmlText = f.read()
    # lxml HTML解析器
    soup = BeautifulSoup(htmlText, 'lxml')
    # print(soup.title)
    print(soup.title.string)

    indexUrl = 'https://lf.qqershou.com'
    with open(f"{floderName}\\analyzeHtml.html", 'w+', encoding='utf-8') as f:
        ulContent = soup.select(
            'ul[class="USltTu stui-vodlist clearfix"]')  # 该标签属性值变化
        for item in ulContent:
            # print(item)
            # item_str = str(item)
            # f.write(item_str)
            for i in item.find_all('li'):
                name = i.find('a')['title']
                print('名字：%s' % name)
                imgUrl = indexUrl+i.find('a')['data-original']
                print('图片url：%s' % imgUrl)
                # print(item.find('a'))
                state = i.find('span').text
                print('状态：%s' % state)
                actions = i.find('p').string
                print('演员：%s' % actions)
                f.write(name)
                f.write(imgUrl)
                f.write(state)
                f.write(actions+'\n')
    print('write done!')


def input_str():
    string = input()
    return string


def clean(floderName):
    abspath = os.getcwd()
    print('当前路径：%s' % abspath)
    rootpath = os.path.abspath('..')
    print('上级目录：%s' % rootpath)
    # 路径拼接
    newPath = os.path.join(abspath, f'{floderName}')
    if not os.path.exists(f"{floderName}"):
        print('no such file or floder：%s' % floderName)
    else:

        key = input('input `y` to continue, other input will be take as no：')
        if key != 'y':
            exit(0)
        shutil.rmtree(newPath)
        time.sleep(0.5)


def main():
    url = 'https://lf.qqershou.com/news/2.html'
    print(f"Enter a website：{url}")
    clean(floderName)
    openUrl(url, floderName, fileName)
    parsingHTML(floderName, fileName)


if __name__ == '__main__':
    floderName = 'resource'
    fileName = 'source'
    imgFloder = 'image'
    data = []
    main()
    print('done')
